package com.ihome.android.market2.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnKeyListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ihome.android.image.CubeImageView;
import com.ihome.android.image.ImageLoader;
import com.ihome.android.academy.R;
import com.ihome.android.market2.info.TemplateContentInfo;

@SuppressLint("NewApi")
public class RankingItem implements OnClickListener, OnFocusChangeListener {
	private final String	TAG	= RankingItem.class.getSimpleName();
	private RelativeLayout	mRelativeLayout;

	private ImageView		iv_frame;
	private CubeImageView	appIcon;
	private TextView		appName;

	private Context			mContext;
	private ImageLoader		imageLoader;
	public int				position;

	public RankingItem(Context context, ImageLoader imageLoader) {
		this.mContext = context;
		this.imageLoader = imageLoader;

		LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mRelativeLayout = (RelativeLayout) inflater.inflate(R.layout.ranking_item, null);
		iv_frame = (ImageView) mRelativeLayout.findViewById(R.id.iv_frame);
		appIcon = (CubeImageView) mRelativeLayout.findViewById(R.id.rank_img);
		appName = (TextView) mRelativeLayout.findViewById(R.id.rank_title);
		mRelativeLayout.setOnFocusChangeListener(this);
	}

	/**
	 * 初始化View，根据AppInfo填充View
	 * 
	 * @param appInfo
	 * @param position
	 */
	public void initView(TemplateContentInfo appInfo, int position) {
		this.position = position;
		if (appInfo != null) {
			if (appInfo != null) {
				if (!TextUtils.isEmpty(appInfo.getPosterUrl())) {
					appIcon.loadImage(imageLoader, appInfo.getPosterUrl());
				}
				else {
					appIcon.setImageResource(R.drawable.icon_default);;
				}
				appName.setText(appInfo.getName());
			}
		}
	}

	public void setClickListener(OnClickListener onClickListener) {
		mRelativeLayout.setOnClickListener(onClickListener);
	}

	public View getView() {
		return mRelativeLayout;
	}

	public void setLayoutParams(RelativeLayout.LayoutParams params) {
		mRelativeLayout.setLayoutParams(params);
	}

	public void setTag(Object tag) {
		mRelativeLayout.setTag(tag);
	}

	public void setOnKeyListener(OnKeyListener onkeylistener) {
		mRelativeLayout.setOnKeyListener(onkeylistener);
	}

	@Override
	public void onClick(View v) {
		//todo: jump to appDetail with appid
	}
	
	@Override
	public void onFocusChange(View v, boolean hasFocus) {
		if (hasFocus) {
			iv_frame.setVisibility(View.VISIBLE);
		}
		else {
			iv_frame.setVisibility(View.INVISIBLE);
		}
	}

}
